﻿namespace RandomGame2
{
    /// <summary>
    /// 随机小游戏：用户需根据显示内容按下上下左右键
    /// </summary>
    internal class Program
    {
        //定义枚举类型的变量Direction
        enum Direction
        {
            Up,
            Down,
            Left,
            Right,
        }
        //设置试验次数为10
        const int CountOfTest = 10;
        static void Main(string[] args)
        {
            //定义计算时间差的变量来统计反应时间
            TimeSpan  reactionTime ;
            //定义变量统计错误次数
            int countOfError = 0;
            //设置随机变量
            Random rnd = new Random();
            for (int i = 0; i < CountOfTest; i++)
            {
                //取当前时间作为试验开始时间
                DateTime startTime = DateTime.Now;
                //清屏
                Console.Clear();                
                Direction direction = (Direction)rnd.Next(0, 4);
                //输出direction并把它转换为string类型
                Console.WriteLine(direction.ToString());
                bool flag = false;
                do
                {
                    //用变量key来接收用户的输入
                    var key = Console.ReadKey();
                    switch (direction)
                    {
                        case Direction.Up:
                            if (key.Key == ConsoleKey.UpArrow) flag = true;
                            else countOfError++;
                            break;
                        case Direction.Down:
                            if (key.Key == ConsoleKey.DownArrow) flag = true;
                            else countOfError++;
                            break;
                        case Direction.Left:
                            if (key.Key == ConsoleKey.LeftArrow) flag = true;
                            else countOfError++;
                            break;
                        case Direction.Right:
                            if (key.Key == ConsoleKey.RightArrow) flag = true;
                            else countOfError++;
                            break;
                    }
                }
                while (flag == false);
                //取测试结束时间
                DateTime finalTime=DateTime.Now;
                //计算试验所用时间
                 reactionTime = finalTime - startTime;
                Console.WriteLine($"现在正在运行第{i}次测试，总耗时{reactionTime}");
                Console.WriteLine($"错误次数为{countOfError}");
            }
        }
    }
}
